package uk.ac.cam.echo.app;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import uk.ac.cam.echo.messages.LocationMessage;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

/**
 * @author Thom Sutcliffe, Tom Smith
 */
public class MapActivity extends SecondaryActivity
{

	private List<LocationMessage> courtList = new LinkedList<LocationMessage>();

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		// Set layout to map.
		setContentView(R.layout.map);

		// get potential options from previous activity
		Bundle extras = getIntent().getExtras();
		final String courtRFID;

		final int spinnerSelected;

		// extract useful information from options (if present)
		if (extras != null)
		{
			courtRFID = extras.getString("courtRFID");

		}
		else
		{
			courtRFID = "";
		}

		List<String> courtListString = new LinkedList<String>();

		try
		{
			courtList = DBInt.getInstance().getLocations();
			courtList.add(new LocationMessage("", "Overall"));

			for (LocationMessage l : courtList)
			{
				courtListString.add(l.toString());
			}

		}
		catch (Exception e)
		{
			showOKMessage("Failed to connect to the database.\n"+e);
		}

		Button homeButton = (Button) findViewById(R.id.maphome);
		Button scheduleButton = (Button) findViewById(R.id.mapschedule);
		final Spinner spinner = (Spinner) findViewById(R.id.courtlist);
		ImageView imageView = (ImageView) findViewById(R.id.mapimageview);

		final Adapter spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, courtListString);

		spinner.setAdapter((SpinnerAdapter) spinnerAdapter);

		if (courtRFID.equals("E007000002A4C3A1"))
		{ // The Street
			imageView.setImageResource(R.drawable.the_street);
			spinnerSelected = 2;
		}
		else if (courtRFID.equals("E0070000029C00AA"))
		{ // Lower Terrace 1
			imageView.setImageResource(R.drawable.first_floor);
			spinnerSelected = 1;
		}
		else if (courtRFID.equals("E00700000299C27C"))
		{ // Lower Terrace 2
			imageView.setImageResource(R.drawable.second_floor);
			spinnerSelected = 0;
		}
		else
		{
			imageView.setImageResource(R.drawable.main_map);
			spinnerSelected = 3;
		}

		spinner.setSelection(spinnerSelected);

		homeButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				goHome();
			}
		});

		/*
		 * functionality for court selected.
		 */
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
		{

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
			{

				if (position != spinnerSelected)
				{
					LocationMessage l = courtList.get(position);

					Intent i = new Intent(self, MapActivity.class);
					i.putExtra("courtRFID", l.getRFID());
					i.putExtra("spinnerPos", position);
					self.startActivity(i);
					self.finish();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
				spinner.setSelection(0);
			}
		});

		scheduleButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent i = new Intent(self, EventsActivity.class);
				i.putExtra("courtRFID", courtRFID);
				i.putExtra("spinnerPos", spinnerSelected);
				self.startActivity(i);
				self.finish();
			}
		});

	}
}
